package controllers;

import models.core.user.FacebookCredentialDtoImpl;
import models.core.user.LocalCredentialDtoImpl;
import models.core.user.UserDto;

import org.apache.commons.lang.StringUtils;

import play.data.Form;
import play.mvc.Controller;
import play.mvc.Http.Cookie;
import play.mvc.Result;
import services.ServiceException;
import services.core.UserService;
import views.html.blocks.auth;
import views.html.blocks.authsuccess;
import controllers.facebook.FacebookManager;


public class Authentication extends Controller {
	
	public static Form<LocalCredentialDtoImpl> signupForm = form(LocalCredentialDtoImpl.class);
	public static Form<UserDto> createForm = form(UserDto.class);

	private final static String SESSION_NAME = "VV_SESSION";
	
	public static String getSessionName(){
		return SESSION_NAME;
	}
	
	public static Result logout(){
		session().clear();
		response().setCookie(SESSION_NAME, "", 0);
		return ok(authsuccess.render());
	}
	
	
	public static Result facebook(){
		Cookie c = request().cookies().get(FacebookManager.getCookieName());
		String token = FacebookManager.getAccessToken(c.value());
		FacebookCredentialDtoImpl credential = new FacebookCredentialDtoImpl();
		credential.setAccessToken(token);
		try {
			String vv_token = UserService.authenticate(credential).get(new Long(10000));
			session(SESSION_NAME,vv_token);
			response().setCookie(SESSION_NAME, vv_token, 1000);
			return ok(authsuccess.render());
		} catch (ServiceException e) {
			return badRequest(e.getMessage());
		}
	}
	
	public static Result login(){
		Form<LocalCredentialDtoImpl> filledForm = signupForm.bindFromRequest();
        if(filledForm.field("login").valueOr("").isEmpty()) {
            filledForm.reject("login", "You must fill your login");
        }
        
        if(filledForm.field("password").valueOr("").isEmpty()) {
            filledForm.reject("password", "You must fill your Password");
         }
                
        if(filledForm.hasErrors()) {
            return badRequest(auth.render(filledForm, createForm));
        } else {
        	LocalCredentialDtoImpl credential = filledForm.get();
        	try{
	        	String vv_token = UserService.authenticate(credential).get();
				if(vv_token != null && StringUtils.isNotEmpty(vv_token)){
					session(SESSION_NAME,vv_token);
					response().setCookie(SESSION_NAME, vv_token, 1000);
				} else {
					filledForm.reject("login", "This User not exist, or bad password");
					return badRequest(auth.render(filledForm, createForm));
				}
	            return ok(authsuccess.render());
	        } catch (ServiceException e) {
    			return badRequest(e.getMessage());
    		}
        }
	}
	
	public static Result create(){
		Form<UserDto> filledForm = createForm.bindFromRequest();
		if(filledForm.field("login").valueOr("").isEmpty()) {
            filledForm.reject("login", "You must fill your login");
        }
        
        if(filledForm.field("password").valueOr("").isEmpty()) {
            filledForm.reject("password", "You must fill your Password");
        }
        if(filledForm.field("firstName").valueOr("").isEmpty()) {
            filledForm.reject("firstName", "You must fill your firstname");
        }
        if(filledForm.field("lastName").valueOr("").isEmpty()) {
            filledForm.reject("lastNmae", "You must fill your lastname");
        }
        if(filledForm.field("email").valueOr("").isEmpty()) {
            filledForm.reject("email", "You must fill your Email");
        }
        if(filledForm.field("repassword").valueOr("").equals(filledForm.field("password"))) {
            filledForm.reject("repassword", "You must fill your Email");
        }
        if(filledForm.hasErrors()) {
            return badRequest(auth.render(signupForm,filledForm));
        } else {
        	UserDto dto = filledForm.get();
        	try {
        		String vv_token = UserService.create(dto).get();
        		if(vv_token != null && StringUtils.isNotEmpty(vv_token)){
					session(SESSION_NAME,vv_token);
					response().setCookie(SESSION_NAME, vv_token, 1000);
	        	}
        	} catch (ServiceException e) {
    			return badRequest(e.getMessage());
    		}
        }
		return ok(authsuccess.render());
	}
	
	public static Result cancel(){
		return null;
	}
	
	public static Result auth(){
		return ok(auth.render(signupForm,createForm));
	}

}
